home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / unix / expnntp / expnntp.pat < prev    next >
Encoding:
Text File  |  1994-06-24  |  13.5 KB  |  499 lines

  1. *** expire.c_d    Sun Apr 17 15:13:10 1994
  2. --- expire.c    Fri Jun 24 14:23:10 1994
  3. ***************
  4. *** 23,28 ****
  5. --- 23,54 ----
  6.    * '/', '\' or '.' to indicate subdirectories.
  7.    * filename should NOT have the ending '.txt'
  8.    */
  9. + /* 25 March 1994
  10. +  * Expire now expires messages stored in the NNTP server format.
  11. +  * the additions to expire.dat 'sort of' follow the conventions for nntp
  12. +  * in other areas of nos, namely the delimiter for nntp groups is bang (!)
  13. +  * thus the format for expiring news items is
  14. +  * !<newsgroup-name> age     for example  !ampr.vk5 21
  15. +  * age still defaults to 21 days if not specified.
  16. +  * automatic active and history file updates are done on each firing of
  17. +  * the expiry process, only if at least one newsgroup was processed.
  18. +  * all records are treated the same in the history file, and are expired
  19. +  * when older than the largest number of days specified for any newsgroup.
  20. +  * numbers of expiries in groups and history are both logged if > 0
  21. +  * if you have any ideas on the above, dont keep them to yourself !! ;-)
  22. +  * this code is not rigorously tested :-) vk5xxx march 1994
  23. +  *
  24. +  * April 7 1994
  25. +  * added test for articles to ensure they are numeric. nos uses other control
  26. +  * files in the newsgroup area (namely news.rc), nntp_expire would expire
  27. +  * these happily, and update_nntp_active wqas confused by them (start = 0)
  28. +  *
  29. +  * June 25 1994
  30. +  * Update the news.rc file with the lowest article number when the news.rc
  31. +  * number is invalid.
  32. +  */
  33.   #ifdef MSDOS
  34.   #include <dir.h>
  35.   #include <dos.h>
  36. ***************
  37. *** 41,46 ****
  38. --- 67,76 ----
  39.   #include "smtp.h"
  40.   #include "socket.h"
  41.   #include "index.h"
  42. + #ifdef NNTPS
  43. + #include "dirutil.h"   /* NNTP expire */
  44. + #include <limits.h>    /* NNTP expire */
  45. + #endif
  46.   
  47.   #ifdef LINUX
  48.   extern int unlink __ARGS((char *));
  49. ***************
  50. *** 99,104 ****
  51. --- 129,144 ----
  52.   static void expire __ARGS((char *,int));
  53.   static void Oldbidtick __ARGS((void *p));
  54.   
  55. + #ifdef NNTPS /* for NNTP expiry */
  56. + /* NNTP Expire stuff */
  57. + static void expire_nntp __ARGS((char *, int));
  58. + static void update_nntp_history __ARGS((int));
  59. + static void update_nntp_active __ARGS((void));
  60. + static void update_newsrc __ARGS((int, int, char *));
  61. + static int convert_num __ARGS((char **));
  62. + static char *newsgroup_to_path __ARGS((char *));
  63. + #endif
  64.   int
  65.   doexpire(argc,argv,p)
  66.   int argc;
  67. ***************
  68. *** 140,145 ****
  69. --- 180,189 ----
  70.   void *v1, *v2;
  71.   {
  72.       char line[80];
  73. + #ifdef NNTPS /* for NNTP expiry */
  74. +     int expire_nntp_history = 0;
  75. + #endif
  76.       int age;
  77.       char *cp;
  78.       FILE *ctl;
  79. ***************
  80. *** 152,158 ****
  81. --- 196,207 ----
  82.       /* read lines from the control file */
  83.       while(fgets(line, sizeof(line), ctl) != NULLCHAR) {
  84.           pwait(NULL); /* be nice */
  85. + #ifdef NNTPS /* for NNTP expiry */
  86.           if((*line == '#') || (*line == '\n')) /* comment or blank line */
  87. + #endif
  88. + #ifndef NNTPS /* for NNTP expiry */
  89. +         if((*line == '#') || (*line == '!') || (*line == '\n')) /* comment or blank line */
  90. + #endif
  91.               continue;
  92.           rip(line);
  93.           /* terminate area name */
  94. ***************
  95. *** 163,174 ****
  96. --- 212,533 ----
  97.               age = atoi(cp);
  98.           }
  99.           pwait(NULL); /* be nice */
  100. + #ifdef NNTPS /* for NNTP expiry */
  101. +         if (*line == '!')   /* Expire NNTP entry if line begins with ! */ {
  102. +             expire_nntp(&line[1], age);
  103. +             if (expire_nntp_history < age)
  104. +                 expire_nntp_history = age;
  105. +         }
  106. +         else
  107. + #endif
  108.               expire(line, age);
  109.       }
  110.       fclose(ctl);
  111. + #ifdef NNTPS /* for NNTP expiry */
  112. +     if (expire_nntp_history) {
  113. +         update_nntp_history(expire_nntp_history);
  114. +         update_nntp_active();  /* and news.rc */
  115. +     }
  116. + #endif
  117.       Eproc = 0;
  118.   }
  119.   
  120. + #ifdef NNTPS /* for NNTP expiry */
  121. + /*
  122. +         expire nntp articles - the kludgomatic version.
  123. +         these routines probably arent memory friendly, but they work.
  124. +         more work needs to be done to make it meld more nicely into nos.
  125. +         coded by brett england (future amateur) and rob vk5xxx.
  126. + */
  127. + static
  128. + int
  129. + convert_num( p )
  130. + char **p;
  131. + {
  132. +     int i;
  133. +     char *pp = *p;
  134. +     i = (*pp - '0') * 10 + *(pp+1) - '0';
  135. +     (*p)+=2;
  136. +     return (i);
  137. + }
  138. + static
  139. + void
  140. + update_nntp_history(age)
  141. + int age;
  142. + {
  143. +   FILE *old, *new;
  144. +   char bckfile[FILE_PATH_SIZE];
  145. +   char line[LINELEN];
  146. +   char *p;
  147. +   struct tm tm_time;
  148. +   time_t expire_time, file_time, now;
  149. +   int history_records_expired = 0;
  150. +   while(mlock(Newsdir,"history"))
  151. +     pause(10000L);
  152. +   expire_time = (long)age * 24L * 60L * 60L;  /* days to seconds */
  153. +   time(&now);
  154. +   /* Rename the history file, and use it to expire messages
  155. +   */
  156. +   sprintf(bckfile,"%s.bak",History);
  157. +   unlink(bckfile);
  158. +   if(rename(History,bckfile) == -1) {
  159. +       log(-1,"NNTP Expire History: Can't rename %s to %s",History, bckfile);
  160. +       return;
  161. +   }
  162. +   if((old = fopen(bckfile,"rt")) == NULL) {
  163. +       log(-1,"NNTP Expire History: Can't open file %s for read", bckfile);
  164. +       return;
  165. +   }
  166. +   if((new = fopen(History,"wt")) == NULL) {
  167. +       log(-1,"NNTP Expire History: Can't open file %s for write",History);
  168. +       return;
  169. +   }
  170. +   for(;;) {
  171. +     if(fgets(line, LINELEN, old) == NULL)
  172. +         break;
  173. +     p = line;
  174. +     while (*p != '\0' && *p != '>')
  175. +        p++;
  176. +     if (*p != '>')  /* Some sort of corrupt line */
  177. +         continue;
  178. +     p+=2;
  179. +     tm_time.tm_year = convert_num(&p);
  180. +     tm_time.tm_mon = convert_num(&p)-1;
  181. +     tm_time.tm_mday = convert_num(&p);
  182. +     p++;   /* Point to time component */
  183. +     tm_time.tm_hour = convert_num(&p);
  184. +     tm_time.tm_min = convert_num(&p);
  185. +     tm_time.tm_sec = 0;
  186. +     file_time = mktime(&tm_time);
  187. +     if (file_time > ( now - expire_time ))
  188. +       fputs(line, new);
  189. +     else
  190. +       history_records_expired++;
  191. +   }
  192. +   fclose(new);
  193. +   fclose(old);
  194. +   rmlock(Newsdir,"history");
  195. +   if(history_records_expired > 0)
  196. +     log(-1,"NNTP History: Expired %d records ...", history_records_expired);
  197. + }
  198. + /* Check the news.rc file if the number contained is less than min
  199. +    replace the news.rc file contents with min.
  200. + */
  201. + static
  202. + void
  203. + update_newsrc( min, max, path )
  204. +   int min, max;
  205. +   char *path;
  206. + {
  207. +   FILE *newsrcfd;
  208. +   char news_file[FILE_PATH_SIZE], line[LINELEN];
  209. +   int no;
  210. +   sprintf(news_file,"%s/news.rc",path);
  211. +   if((newsrcfd = fopen(news_file,"rt")) == NULL) {
  212. +       log(-1,"NNTP News: Can't open file %s for read", news_file);
  213. +       return;
  214. +   }
  215. +   fgets(line, LINELEN, newsrcfd);
  216. +   fclose(newsrcfd);
  217. +   no = atoi(line);
  218. +   if((no < min) || (no > max)) {
  219. +     if((newsrcfd = fopen(news_file,"wt")) == NULL) {
  220. +         log(-1,"NNTP News: Can't open file %s for write", news_file);
  221. +         return;
  222. +     }
  223. +     fprintf(newsrcfd,"%d",min);
  224. +     fclose(newsrcfd);
  225. +   }
  226. + }
  227. + /* Update the active file and return the lowest news article
  228. + */
  229. + static
  230. + void
  231. + update_nntp_active() {
  232. +   FILE *old, *new;
  233. +   char bckfile[FILE_PATH_SIZE];
  234. +   char line[LINELEN];
  235. +   char *p, *path, *rpath;
  236. +   int min, max;
  237. +   int command, file_num;
  238. +   struct ffblk *file;
  239. +   /* Rename the active file
  240. +   */
  241. +   sprintf(bckfile,"%s.bak",Active);
  242. +   unlink(bckfile);
  243. +   if(rename(Active,bckfile) == -1) {
  244. +       log(-1,"NNTP Active: Can't rename %s to %s",Active, bckfile);
  245. +       return;
  246. +   }
  247. +   if((old = fopen(bckfile,"rt")) == NULL) {
  248. +       log(-1,"NNTP Active: Can't open file %s for read", bckfile);
  249. +       return;
  250. +   }
  251. +   if((new = fopen(Active,"wt")) == NULL) {
  252. +       log(-1,"NNTP Active: Can't open file %s for write",Active);
  253. +       return;
  254. +   }
  255. +   file = (struct ffblk *)mallocw(sizeof(struct ffblk));
  256. +   for(;;) {
  257. +     if(fgets(line, LINELEN, old) == NULL)
  258. +        break;
  259. +     p = line;
  260. +     while (*p != '\0' && ! isspace(*p))
  261. +       p++;
  262. +     if (*p == '\0')
  263. +       continue;
  264. +     *p = '\0';
  265. +     p++;
  266. +     while(*p != '\0' && (isspace(*p) || isdigit(*p)))
  267. +         p++;
  268. +     rpath = newsgroup_to_path( line );
  269. +     path = wildcardize(rpath);
  270. +     command = 0;
  271. +     min = INT_MAX;
  272. +     max = INT_MIN;
  273. +     for(;;) {
  274. +         if (!nextname(command, path, file))
  275. +             break;
  276. +         command = 1;   /* Find next */
  277. +         if (file->ff_name[0] == '.')
  278. +             continue;  /* ignore . and .. */
  279. +         file_num = atoi(file->ff_name);
  280. +         if (file_num > 0) {     /* should always be greater than 0 */
  281. +             if (file_num < min)
  282. +                 min = file_num;
  283. +             if (file_num > max)
  284. +                 max = file_num;
  285. +         } 
  286. +     }
  287. +     if (min == INT_MAX && max == INT_MIN) { /* No files */
  288. +         min = 1;
  289. +         max = 0;
  290. +     }
  291. +     update_newsrc(min, max, rpath);
  292. +     fprintf(new,"%s %05d %05d %s", line, max, min, p);
  293. +   }
  294. +   fclose(new);
  295. +   fclose(old);
  296. +   free(file);
  297. + }
  298. + static
  299. + char
  300. + *newsgroup_to_path( group )
  301. + char *group;
  302. + {
  303. +     FILE *f;
  304. +     static char line[LINELEN];
  305. +     char *cp;
  306. +     if((f = fopen(Pointer,"r")) == NULL)
  307. +         return((char *)NULL);
  308. +     for (;;) {
  309. +         if (fgets(line,LINELEN,f) == NULL)
  310. +             break;
  311. +         if (strcspn(line," ") != strlen(group))
  312. +             continue;
  313. +         if (strnicmp(group,line,strlen(group)) == 0) {
  314. +             cp = (strchr(line,' ')) + 1;
  315. +             rip(cp);
  316. +             fclose(f);
  317. +             return (cp);
  318. +         }
  319. +     }
  320. +     fclose(f);
  321. +     return (NULL);
  322. + }
  323. + void
  324. + expire_nntp(nntp_name, age)
  325. + char *nntp_name;
  326. + int age;
  327. + {
  328. +     char *path;
  329. +     int command = 0;
  330. +     struct ffblk file;
  331. +     struct tm tm_time;
  332. +     time_t file_time, expire_time, now;
  333. +     char nntp_file[FILE_PATH_SIZE];
  334. +     char save_path[FILE_PATH_SIZE];
  335. +     int file_num, expired = 0;
  336. +     /* Resolve nntp name into directory path */
  337. +     if ((path = newsgroup_to_path( nntp_name )) == NULL)
  338. +         return;
  339. +     expire_time = (long)age * 24L * 60L * 60L;
  340. +     time(&now);
  341. +     strcpy(save_path, path);
  342. +     path = wildcardize(path);
  343. +     for(;;) {
  344. +         if (!nextname(command, path, &file))
  345. +             break;
  346. +         command = 1;   /* Find next */
  347. +         if (file.ff_name[0] == '.')
  348. +             continue;  /* ignore . and .. */
  349. +         file_num = atoi(file.ff_name);
  350. +         /* should always be greater than 0 for a valid nntp article */
  351. +         if (file_num > 0) {
  352. +             /* only expire a file that is numeric :-) */
  353. +             tm_time.tm_sec = 0;  /* DOS doesn't store this */
  354. +             tm_time.tm_min = (file.ff_ftime >> 5) & 0x3f;
  355. +             tm_time.tm_hour = (file.ff_ftime >> 11) & 0x1f;
  356. +             tm_time.tm_mday = file.ff_fdate & 0x1f;
  357. +             tm_time.tm_mon = ((file.ff_fdate >> 5) & 0xf)-1;
  358. +             tm_time.tm_year = (file.ff_fdate >> 9) + 80;
  359. +             file_time = mktime(&tm_time);
  360. +             if (( now - expire_time ) > file_time) {
  361. +                  sprintf(nntp_file, "%s/%s", save_path, file.ff_name);
  362. +                  unlink(nntp_file);
  363. +                  expired++;
  364. +             }
  365. +         }
  366. +     }
  367. +     if(expired)
  368. +         log(-1,"NNTP Expired: %d in %s",expired, nntp_name);
  369. + }
  370. + #endif /*nntps*/
  371.   void
  372.   expire(filename,age)
  373.   char *filename;
  374. *** dirutil.h_d    Sun Apr 17 15:15:26 1994
  375. --- dirutil.h    Fri Jun 24 13:41:42 1994
  376. ***************
  377. *** 34,39 ****
  378. --- 34,41 ----
  379.   extern int findfirst(char *pat, struct ffblk *ff, int attr);
  380.   extern int findnext(struct ffblk *ff);
  381.   
  382. + #else
  383. + #include <dir.h>
  384.   #endif
  385.   
  386.   struct cur_dirs {
  387. ***************
  388. *** 55,60 ****
  389. --- 57,67 ----
  390.   void free_dirs(struct cur_dirs * dirs);
  391.   int dir_ok(char * path,struct cur_dirs * dirs);
  392.   int dircmd __ARGS((int argc,char *argv[],void *p));
  393. + /* Made public for nntp expiry */
  394. + int nextname __ARGS((int command, char *name, struct ffblk *sbuf));
  395. + char *wildcardize __ARGS((char *path));
  396.   #ifdef MSDOS
  397.   int dosfnchr __ARGS((int ch));
  398.   #endif
  399. *** dirutil.c_d    Sun Apr 17 15:13:06 1994
  400. --- dirutil.c    Fri Jun 24 12:01:34 1994
  401. ***************
  402. *** 55,64 ****
  403.   #ifdef  notdef
  404.   static int getdir_nosort __ARGS((char *path,int full,FILE *file));
  405.   #endif
  406. - static int nextname __ARGS((int command, char *name, struct ffblk *sbuf));
  407.   static void print_free_space __ARGS((char *path,FILE *file,int n));
  408.     
  409. - static char *wildcardize __ARGS((char *path));
  410.   extern void crunch __ARGS((char *buf,char *path));
  411.     
  412.   #define REGFILE (FA_DIREC)
  413. --- 55,62 ----
  414. ***************
  415. *** 85,91 ****
  416.   }
  417.     
  418.   /* find the first or next file and lowercase it. */
  419. ! static int
  420.   nextname(command, name, sbuf)
  421.   int command;
  422.   char *name;
  423. --- 83,89 ----
  424.   }
  425.     
  426.   /* find the first or next file and lowercase it. */
  427. ! int
  428.   nextname(command, name, sbuf)
  429.   int command;
  430.   char *name;
  431. ***************
  432. *** 305,311 ****
  433.   }
  434.     
  435.   /* fix up the filename so that it contains the proper wildcard set */
  436. ! static char *
  437.   wildcardize(path)
  438.   char *path;
  439.   {
  440. --- 303,309 ----
  441.   }
  442.     
  443.   /* fix up the filename so that it contains the proper wildcard set */
  444. ! char *
  445.   wildcardize(path)
  446.   char *path;
  447.   {
  448.